import React from 'react';
import { Container, Header, Content, List, ListItem, Thumbnail, Text, Body,
Footer,Item,Input,Button
} from 'native-base';
import imageCommon from '../img/common.jpg';
import AppState from './AppState';

export default class Commentaries extends React.Component {
  constructor(props){
    super(props);
    this.state ={
      CommentsContent:[{
        content: "我觉得还ok", 
        userName: "riguang", 
        portrait: "img/common.png",
        hid:0
      }],
      inputText:""
    }
  }
  getComments = ()=>{
    return fetch('http://106.14.127.114:9999/request.ashx',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: 'cmd=GetComments&hid='+this.props.index
    });
  }
  publishComment = ()=>{
    let hid = this.state.CommentsContent[0].hid;
    let inputText = this.state.inputText;
    let uid = AppState.nowUser.uid;
    let userName = AppState.nowUser.userName;
    fetch('http://106.14.127.114:9999/request.ashx',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: 'cmd=PublishComment&commentContent='+inputText+"&hid="+hid+"&uid="+uid
    })
    .then((response) => {
      return response.text();
    })
    .then((responseText)=>{
      console.log(responseText);
      if(responseText==="success"){
        this.state.CommentsContent.push({
          content: inputText, 
          userName: userName, 
          portrait: "img/common.png",
          hid:hid
        });
        this.setState({
          inputText:"",
          CommentsContent:this.state.CommentsContent,
      });
      //评论数+1还没写
      for(let value of AppState.HomeData){
       if(value.hid===hid){
          value.CommentsSum+=1;
          break;
       } 
      }
      }
    })
    .catch((error) => {
      console.error(error);
    });
  }
  componentWillMount(){
    this.getComments().then((response) => response.json())
    .then((responseJson) => {
      this.setState({
        CommentsContent:responseJson
      });
      console.log(responseJson);
    })
    .catch((error) => {
      console.error(error);
    });
  }
  render() {
    const host = "http://106.14.127.114:9999/";
    const {CommentsContent} = this.state;
    return (
      <Container>
        <Header />
        <Content>
          <List>
          {CommentsContent.map((keys,index)=>
            <ListItem key={index}>
              <Thumbnail square size={80} source={{uri:host+keys.portrait}} />
              <Body>
                <Text>{keys.userName}</Text>
                <Text note>{keys.content}</Text>
              </Body>
            </ListItem>
          )}
          </List>
          <Item>
            <Input placeholder="你想说点什么？"
            onChangeText={(inputText) => this.setState({inputText})}
            value={this.state.inputText}
            />
          </Item>
          <Button full onPress={this.publishComment}>
            <Text>发表</Text>
          </Button>
        </Content>
      </Container>
    );
  }
}
